(function() {

  $.widget("ui.FileUploader", {
    _create: function() {
      var div, path, value;
      div = $(this.element).closest("div");
      div.addClass("file-upload");
      value = $(this.element).val();
      if (value && value.length) {
        div.append('<a class="no-link" href="' + value + '">Download</a>');
      }
      path = Settings.UploadPath;
      div.append('<span><input type="file" value=""/><span>Upload</span></span>');
      return div.find("span input").fileupload({
        dataType: "text",
        url: SiteMap.Shared.FileUpload,
        sequentialUploads: true,
        start: function(e) {
          return Loading.Start();
        },
        done: function(e, data) {
          $(this).closest(".image-upload").find("input[type = hidden]").val(data.result);
          Messages.WriteMessage("File uploaded successfully");
          $(this).closest(".file-upload").find("a").remove();
          $(this).closest(".file-upload").append('<a class="no-link" href="' + path + data.result + '">Download</a>');
          $(this).closest(".file-upload").find("input[type=hidden]").val(data.result).closest("fieldset>div").trigger("validate");
          return Loading.Stop();
        },
        fail: function(e, data) {
          Messages.WriteMessage("Unexpected error");
          return Loading.Stop();
        }
      });
    }
  });

}).call(this);
